package com.china08.yxyapi.service.res.statistics.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.entity.db.res.YxyArea;
import com.china08.yxyapi.entity.db.statistics.AreaResourceReport;
import com.china08.yxyapi.entity.mg.statistics.SchoolUploadResourceDay;
import com.china08.yxyapi.entity.mg.statistics.SchoolUploadResourceReport;
import com.china08.yxyapi.entity.mg.statistics.UserUploadResourceMonth;
import com.china08.yxyapi.repository.db.statistics.AreaResourceReportRepository;
import com.china08.yxyapi.repository.db.yxy.YxyAreaRepository;
import com.china08.yxyapi.repository.mg.SchoolUploadResourceReportRepository;
import com.china08.yxyapi.repository.mg.statistics.SchoolUploadResDayRepository;
import com.china08.yxyapi.repository.mg.statistics.UserUploadResourceMonthRepository;
import com.china08.yxyapi.service.res.statistics.IResourceStatistics;
import com.china08.yxyapi.util.DateUtils;

@Service
public class ResourceStatisticsImpl implements IResourceStatistics {

    private static final Logger LOGGER = Logger.getLogger(ResourceStatisticsImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private AreaResourceReportRepository areaResourceReportRepository;

    @Autowired
    private SchoolUploadResourceReportRepository schoolUploadResourceReportRepository;

    @Autowired
    private SchoolUploadResDayRepository schoolUploadResDayRepository;

    @Autowired
    private YxyAreaRepository yxyAreaRepository;

    @Autowired
    private UserUploadResourceMonthRepository userUploadResourceMonthRepository;

    /*
     * (non-Javadoc)
     * 
     * @see com.china08.yxyapi.service.res.statistics.IResourceStatistics#
     * areaStatistics(java.lang.Integer)
     */
    @Override
    public void areaStatistics(Integer date) {
        String ressql = "SELECT " + " district_code,                             "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         "
                + " FROM yxy_res.yxy_res_resource                                "
                + " WHERE created_date >                                     " + date
                + "  AND created_date <                                     " + (date + 1)
                + " AND type_id != 'YX14'                                        "
                + " AND is_delete = 0                                             "
                + " GROUP BY district_code                                        ";

        List<Map<String, Object>> reslist = jdbcTemplate.queryForList(ressql);
        if (reslist != null && reslist.size() > 0) {
            AreaResourceReport entity = null;
            for (Map<String, Object> o : reslist) {

                AreaResourceReport areaResourceReport = areaResourceReportRepository
                        .findByIntDateAndAreaIdDistrict(date, (String) o.get("district_code"));
                if (areaResourceReport != null) {
                    areaResourceReport.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    areaResourceReport.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0L : ((BigDecimal) o.get("shard")).longValue());
                    areaResourceReportRepository.save(areaResourceReport);
                } else {

                    entity = new AreaResourceReport();
                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));
                    entity.setIntDate(date);
                    entity.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0L : ((BigDecimal) o.get("shard")).longValue());
                    try {
                        entity.setStatisticsDate(DateUtils.formatStrDate4(date));
                    } catch (ParseException e) {

                    }
                    areaResourceReportRepository.save(entity);
                }
            }
        }

        String weikesql = "SELECT " + " district_code,                                "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + date + "  AND created_date < " + (date + 1)
                + " AND type_id = 'YX14'                                         "
                + " AND is_delete = 0                                            "
                + " GROUP BY district_code                                       ";
        List<Map<String, Object>> weikelist = jdbcTemplate.queryForList(weikesql);
        if (weikelist != null && weikelist.size() > 0) {
            AreaResourceReport entity = null;
            for (Map<String, Object> o : weikelist) {

                AreaResourceReport areaResourceReport = areaResourceReportRepository
                        .findByIntDateAndAreaIdDistrict(date, (String) o.get("district_code"));
                if (areaResourceReport != null) {
                    areaResourceReport.setWeikeCount(o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    areaResourceReport.setShareWeikeCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());
                    areaResourceReportRepository.save(areaResourceReport);
                } else {
                    entity = new AreaResourceReport();
                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));
                    entity.setIntDate(date);
                    entity.setWeikeCount(o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareWeikeCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());
                    try {
                        entity.setStatisticsDate(DateUtils.formatStrDate4(date));
                    } catch (ParseException e) {

                    }
                    areaResourceReportRepository.save(entity);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.china08.yxyapi.service.res.statistics.IResourceStatistics#
     * schoolStatistics(java.lang.Integer)
     */
    @Override
    public void schoolStatistics(Integer date) {
        String ressql = "SELECT " + "  uploader_school_id, uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + date + "  AND created_date < " + (date + 1)
                + " AND type_id != 'YX14'                                        "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";

        List<Map<String, Object>> reslist = jdbcTemplate.queryForList(ressql);
        if (reslist != null && reslist.size() > 0) {
            SchoolUploadResourceReport entity = null;
            SchoolUploadResourceReport areaResourceReport = null;
            for (Map<String, Object> o : reslist) {

                areaResourceReport = schoolUploadResourceReportRepository
                        .findById((String) o.get("uploader_school_id"));
                if (areaResourceReport != null) {

                    // 学校统计的是总数量.
                    long resCount = areaResourceReport.getResCount() == null ? 0 : areaResourceReport.getResCount();
                    resCount = resCount + ((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));

                    long resShareCount = areaResourceReport.getShareResCount() == null ? 0
                            : areaResourceReport.getShareResCount();
                    resShareCount = resShareCount
                            + (o.get("shard") == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    areaResourceReport.setResCount(resCount);
                    areaResourceReport.setShareResCount(resShareCount);

                    // 初始化值为 0, 排序需要
                    if (null == areaResourceReport.getShareWeikeCount()) {
                        areaResourceReport.setShareWeikeCount(0l);
                    }
                    if (null == areaResourceReport.getWeikeCount()) {
                        areaResourceReport.setWeikeCount(0l);
                    }

                    schoolUploadResourceReportRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceReport();
                    entity.setId((String) o.get("uploader_school_id"));
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));
                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));

                    entity.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    // 初始化值为 0, 排序需要
                    entity.setShareWeikeCount(0l);
                    entity.setWeikeCount(0l);

                    if (StringUtils.isNotBlank((String) o.get("uploader_school_id"))) {
                        ResponseEntity<List<String>> reList = restTemplate.exchange(
                                yxyApiYxUrl + "/comm/school/type/id/" + (String) o.get("uploader_school_id"),
                                HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
                                });

                        if (reList != null && reList.getBody() != null && reList.getBody().size() > 0) {
                            entity.setStage(reList.getBody());
                        }
                    }
                    schoolUploadResourceReportRepository.save(entity);
                }
            }
        }

        String weikesql = "SELECT " + " uploader_school_id,uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + date + "  AND created_date < " + (date + 1)
                + " AND type_id = 'YX14'                                         "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";
        List<Map<String, Object>> weikelist = jdbcTemplate.queryForList(weikesql);
        if (weikelist != null && weikelist.size() > 0) {
            SchoolUploadResourceReport entity = null;
            SchoolUploadResourceReport areaResourceReport = null;
            for (Map<String, Object> o : weikelist) {

                areaResourceReport = schoolUploadResourceReportRepository
                        .findById((String) o.get("uploader_school_id"));
                if (areaResourceReport != null) {

                    long weikeCount = areaResourceReport.getWeikeCount() == null ? 0
                            : areaResourceReport.getWeikeCount();
                    weikeCount = weikeCount + ((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));

                    long weikeShareCount = areaResourceReport.getShareWeikeCount() == null ? 0
                            : areaResourceReport.getShareWeikeCount();
                    weikeShareCount = weikeShareCount
                            + (o.get("shard") == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    areaResourceReport.setWeikeCount(weikeCount);
                    areaResourceReport.setShareWeikeCount(weikeShareCount);

                    // 初始化值为 0, 排序需要
                    if (null == areaResourceReport.getShareResCount()) {
                        areaResourceReport.setShareResCount(0l);
                    }
                    if (null == areaResourceReport.getResCount()) {
                        areaResourceReport.setResCount(0l);
                    }

                    schoolUploadResourceReportRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceReport();
                    entity.setId((String) o.get("uploader_school_id"));
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));

                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));

                    entity.setWeikeCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareWeikeCount(
                            (o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    // 初始化值为 0, 排序需要
                    entity.setResCount(0l);
                    entity.setShareResCount(0l);

                    if (StringUtils.isNotBlank((String) o.get("uploader_school_id"))) {
                        ResponseEntity<List<String>> reList = restTemplate.exchange(
                                yxyApiYxUrl + "/comm/school/type/id/" + (String) o.get("uploader_school_id"),
                                HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
                                });
                        if (reList != null && reList.getBody() != null && reList.getBody().size() > 0) {
                            entity.setStage(reList.getBody());
                        }
                    }
                    schoolUploadResourceReportRepository.save(entity);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.china08.yxyapi.service.res.statistics.IResourceStatistics#
     * schoolStatisticsForDay(java.lang.Integer) 每日学校资源上传统计
     */
    @Override
    public void schoolStatisticsForDay(Integer date) {
        String ressql = "SELECT " + "  uploader_school_id, uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + date + "  AND created_date < " + (date + 1)
                + " AND type_id != 'YX14'                                        "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";

        List<Map<String, Object>> reslist = jdbcTemplate.queryForList(ressql);
        if (reslist != null && reslist.size() > 0) {
            SchoolUploadResourceDay entity = null;
            SchoolUploadResourceDay areaResourceReport = null;
            for (Map<String, Object> o : reslist) {

                areaResourceReport = schoolUploadResDayRepository
                        .findById((String) o.get("uploader_school_id") + "_" + date);
                if (areaResourceReport != null) {

                    // 学校统计的是总数量.
                    /*
                     * long resCount = areaResourceReport.getResCount() == null
                     * ? 0 : areaResourceReport.getResCount();
                     * 
                     * long resShareCount =
                     * areaResourceReport.getShareResCount() == null ? 0 :
                     * areaResourceReport.getShareResCount();
                     */
                    areaResourceReport.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    areaResourceReport.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());
                    /*
                     * areaResourceReport.setResCount(resCount);
                     * areaResourceReport.setShareResCount(resShareCount);
                     */

                    schoolUploadResDayRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceDay();
                    entity.setId((String) o.get("uploader_school_id") + "_" + date);
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));
                    try {
                        entity.setStatisticsDate(DateUtils.formatStrDate4(date));
                    } catch (ParseException e) {
                    }
                    entity.setIntDate(date);
                    entity.setIntMonth(Integer.parseInt(StringUtils.substring("" + date, 0, 6)));
                    entity.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    schoolUploadResDayRepository.save(entity);
                }
            }
        }

        String weikesql = "SELECT " + " uploader_school_id,uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + date + "  AND created_date < " + (date + 1)
                + " AND type_id = 'YX14'                                          "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";
        List<Map<String, Object>> weikelist = jdbcTemplate.queryForList(weikesql);
        if (weikelist != null && weikelist.size() > 0) {
            SchoolUploadResourceDay entity = null;
            SchoolUploadResourceDay areaResourceReport = null;
            for (Map<String, Object> o : weikelist) {

                areaResourceReport = schoolUploadResDayRepository
                        .findById((String) o.get("uploader_school_id") + "_" + date);
                if (areaResourceReport != null) {

                    /*
                     * long weikeCount = areaResourceReport.getWeikeCount() ==
                     * null ? 0 : areaResourceReport.getWeikeCount();
                     * 
                     * long weikeShareCount =
                     * areaResourceReport.getShareWeikeCount() == null ? 0 :
                     * areaResourceReport.getShareWeikeCount();
                     */

                    areaResourceReport.setWeikeCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    areaResourceReport.setShareWeikeCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    /*
                     * areaResourceReport.setWeikeCount(weikeCount);
                     * areaResourceReport.setShareWeikeCount(weikeShareCount);
                     */

                    schoolUploadResDayRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceDay();
                    entity.setId((String) o.get("uploader_school_id") + "_" + date);
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));
                    try {
                        entity.setStatisticsDate(DateUtils.formatStrDate4(date));
                    } catch (ParseException e) {
                    }
                    entity.setIntDate(date);
                    entity.setIntMonth(Integer.parseInt(StringUtils.substring("" + date, 0, 6)));
                    entity.setWeikeCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareWeikeCount(
                            (o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    schoolUploadResDayRepository.save(entity);
                }
            }
        }
    }

    // 获取每月每个用户所上传与分享的资源
    @Override
    public void userStatisticsForMonth(int monthAndYear) {

        Date staDate = null;
        try {
            staDate = DateUtils.formatIntDate5(monthAndYear);
        } catch (ParseException e) {

        }
        if (staDate == null) {
            staDate = new Date();
        }

        // 月初
        int startTime = DateUtils.formatDate4(DateUtils.getFirstDay(DateUtils.getMothOfFirstDay(staDate)));
        // 月末
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(staDate);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        int endTime = DateUtils.formatDate4(calendar.getTime());
        String monthSql = " SELECT uploader_Id, uploader_name,uploader_School_Id,uploader_school_name, "
                + " COUNT(1) upload, " + " SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard, "
                + " SUM(IF(type_id = 'YX14',1,0)) weike," + " SUM(IF(type_id != 'YX14',1,0)) res, "
                + " SUM(IF((view_scope = 1 OR view_scope = 2) AND type_id = 'YX14',1,0)) weikeshard, "
                + " SUM(IF((view_scope = 1 OR view_scope = 2) AND type_id != 'YX14',1,0)) resshard "
                + "     FROM yxy_res.yxy_res_resource                                         "
                + "         WHERE upload_time > " + startTime + " AND upload_time < " + endTime + " AND is_delete = 0"
                + "             GROUP BY uploader_Id,uploader_School_Id";

        List<Map<String, Object>> monthlist = jdbcTemplate.queryForList(monthSql);
        if (monthlist != null && monthlist.size() > 0) {
            for (Map<String, Object> map : monthlist) {
                // 查询到数据插入数据库中
                UserUploadResourceMonth uurm = new UserUploadResourceMonth();
                Integer monthDate = Integer.parseInt(DateUtils.formatDate4(staDate).toString().substring(0, 6));
                String userId = map.get("uploader_Id").toString();
                String schoolId = map.get("uploader_School_Id").toString();
                String id = userId + schoolId + monthDate;
                UserUploadResourceMonth uurmData = userUploadResourceMonthRepository.findOne(id);
                if (uurmData == null) {
                    uurm.setId(id);
                    uurm.setUserNick(map.get("uploader_name").toString());
                    uurm.setIntMonth(monthDate);
                    uurm.setStatisticsDate(staDate);
                    uurm.setSchoolId(schoolId);
                    uurm.setSchoolNick(map.get("uploader_school_name").toString());
                    uurm.setResCount(Long.valueOf(map.get("res").toString()));
                    uurm.setShareResCount(Long.valueOf(map.get("resshard").toString()));
                    uurm.setWeikeCount(Long.valueOf(map.get("weike").toString()));
                    uurm.setShareWeikeCount(Long.valueOf(map.get("weikeshard").toString()));
                    uurm.setSynch(0);
                    uurm.setUserId(userId);
                    userUploadResourceMonthRepository.save(uurm);
                } else {
                    int flag = 0;
                    if (uurmData.getResCount() != Long.valueOf(map.get("res").toString())) {
                        uurmData.setResCount(Long.valueOf(map.get("res").toString()));
                        flag++;
                    }

                    if (uurmData.getShareResCount() != Long.valueOf(map.get("resshard").toString())) {
                        uurmData.setShareResCount(Long.valueOf(map.get("resshard").toString()));
                        flag++;
                    }

                    if (uurmData.getWeikeCount() != Long.valueOf(map.get("weike").toString())) {
                        uurmData.setWeikeCount(Long.valueOf(map.get("weike").toString()));
                        flag++;
                    }

                    if (uurmData.getShareWeikeCount() != Long.valueOf(map.get("weikeshard").toString())) {
                        uurmData.setShareWeikeCount(Long.valueOf(map.get("weikeshard").toString()));
                        flag++;
                    }
                    if (flag != 0) {
                        uurmData.setSynch(0);
                        userUploadResourceMonthRepository.save(uurmData);
                    }
                }
            }
        }
    }

    /*
     * 学校统计数据修复方式, 直接统计一个时间端的资源总数,然后更新数据
     * 
     * @see com.china08.yxyapi.service.res.statistics.IResourceStatistics#
     * schoolStatisticsTotal(java.lang.Integer, java.lang.Integer)
     */
    @Override
    public void schoolStatisticsTotal(Integer beginDate, Integer endDate) {
        LOGGER.info(beginDate + "_" + endDate);
        String ressql = "SELECT " + "  uploader_school_id, uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + beginDate + "  AND created_date < " + endDate
                + " AND type_id != 'YX14'                                        "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";

        List<Map<String, Object>> reslist = jdbcTemplate.queryForList(ressql);
        if (reslist != null && reslist.size() > 0) {
            SchoolUploadResourceReport entity = null;
            SchoolUploadResourceReport areaResourceReport = null;
            for (Map<String, Object> o : reslist) {

                areaResourceReport = schoolUploadResourceReportRepository
                        .findById((String) o.get("uploader_school_id"));
                LOGGER.info("statistics schoolId: " + o.get("uploader_school_id"));
                if (areaResourceReport != null) {

                    // 学校统计的是总数量.
                    long resCount = areaResourceReport.getResCount() == null ? 0 : areaResourceReport.getResCount();
                    resCount = (Long) o.get("upload") == null ? resCount : (Long) o.get("upload");

                    long resShareCount = areaResourceReport.getShareResCount() == null ? 0
                            : areaResourceReport.getShareResCount();
                    resShareCount = (o.get("shard") == null ? resShareCount
                            : ((BigDecimal) o.get("shard")).longValue());

                    areaResourceReport.setResCount(resCount);
                    areaResourceReport.setShareResCount(resShareCount);

                    // 初始化值为 0, 排序需要
                    if (null == areaResourceReport.getShareWeikeCount()) {
                        areaResourceReport.setShareWeikeCount(0l);
                    }
                    if (null == areaResourceReport.getWeikeCount()) {
                        areaResourceReport.setWeikeCount(0l);
                    }

                    schoolUploadResourceReportRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceReport();
                    entity.setId((String) o.get("uploader_school_id"));
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));
                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));

                    entity.setResCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareResCount(
                            ((BigDecimal) o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    // 初始化值为 0, 排序需要
                    entity.setShareWeikeCount(0l);
                    entity.setWeikeCount(0l);

                    if (StringUtils.isNotBlank((String) o.get("uploader_school_id"))) {
                        ResponseEntity<List<String>> reList = restTemplate.exchange(
                                yxyApiYxUrl + "/comm/school/type/id/" + (String) o.get("uploader_school_id"),
                                HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
                                });
                        if (reList != null && reList.getBody() != null && reList.getBody().size() > 0) {
                            entity.setStage(reList.getBody());
                        }
                    }

                    schoolUploadResourceReportRepository.save(entity);
                }
            }
        }

        String weikesql = "SELECT " + " uploader_school_id,uploader_school_name, district_code, "
                + "  city_code,                                                  "
                + "  province_code ,                                             "
                + "  COUNT(1) upload,                                            "
                + "  SUM(IF((view_scope = 1 OR view_scope=2),1,0)) shard         " + " FROM yxy_res.yxy_res_resource  "
                + " WHERE created_date > " + beginDate + "  AND created_date < " + endDate
                + " AND type_id = 'YX14'                                         "
                + " AND is_delete = 0                                            "
                + " GROUP BY uploader_school_id                                        ";
        List<Map<String, Object>> weikelist = jdbcTemplate.queryForList(weikesql);
        if (weikelist != null && weikelist.size() > 0) {
            SchoolUploadResourceReport entity = null;
            SchoolUploadResourceReport areaResourceReport = null;
            for (Map<String, Object> o : weikelist) {

                areaResourceReport = schoolUploadResourceReportRepository
                        .findById((String) o.get("uploader_school_id"));
                if (areaResourceReport != null) {

                    long weikeCount = areaResourceReport.getWeikeCount() == null ? 0
                            : areaResourceReport.getWeikeCount();
                    weikeCount = ((Long) o.get("upload") == null ? weikeCount : (Long) o.get("upload"));

                    long weikeShareCount = areaResourceReport.getShareWeikeCount() == null ? 0
                            : areaResourceReport.getShareWeikeCount();
                    weikeShareCount = (o.get("shard") == null ? weikeShareCount
                            : ((BigDecimal) o.get("shard")).longValue());

                    areaResourceReport.setWeikeCount(weikeCount);
                    areaResourceReport.setShareWeikeCount(weikeShareCount);

                    // 初始化值为 0, 排序需要
                    if (null == areaResourceReport.getShareResCount()) {
                        areaResourceReport.setShareResCount(0l);
                    }
                    if (null == areaResourceReport.getResCount()) {
                        areaResourceReport.setResCount(0l);
                    }

                    schoolUploadResourceReportRepository.save(areaResourceReport);
                } else {

                    entity = new SchoolUploadResourceReport();
                    entity.setId((String) o.get("uploader_school_id"));
                    entity.setSchoolId((String) o.get("uploader_school_id"));
                    entity.setSchoolNick((String) o.get("uploader_school_name"));

                    entity.setAreaIdDistrict((String) o.get("district_code"));
                    entity.setAreaIdCity((String) o.get("city_code"));
                    entity.setAreaIdProvince((String) o.get("province_code"));

                    YxyArea city = yxyAreaRepository.findByAreaId((String) o.get("city_code"));
                    YxyArea district = yxyAreaRepository.findByAreaId((String) o.get("district_code"));
                    YxyArea province = yxyAreaRepository.findByAreaId((String) o.get("province_code"));

                    entity.setAreaNameCity(city != null ? city.getAreaName() : "");
                    entity.setAreaNameDistrict(district != null ? district.getAreaName() : "");
                    entity.setAreaNameProvince(province != null ? province.getAreaName() : "");

                    entity.setCreatedDate(new Timestamp(new Date().getTime()));

                    entity.setWeikeCount((Long) o.get("upload") == null ? 0l : (Long) o.get("upload"));
                    entity.setShareWeikeCount(
                            (o.get("shard")) == null ? 0l : ((BigDecimal) o.get("shard")).longValue());

                    // 初始化值为 0, 排序需要
                    entity.setResCount(0l);
                    entity.setShareResCount(0l);

                    if (StringUtils.isNotBlank((String) o.get("uploader_school_id"))) {// 判断学校是否空
                        ResponseEntity<List<String>> reList = restTemplate.exchange(
                                yxyApiYxUrl + "/comm/school/type/id/" + (String) o.get("uploader_school_id"),
                                HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
                                });
                        if (reList != null && reList.getBody() != null && reList.getBody().size() > 0) {
                            entity.setStage(reList.getBody());
                        }
                    }
                    schoolUploadResourceReportRepository.save(entity);
                }
            }
        }
    }
}
